package cn.iocoder.yudao.module.eval.controller.admin.filingcreation;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.eval.controller.admin.filingcreation.vo.*;
import cn.iocoder.yudao.module.eval.dal.dataobject.filingcreation.FilingCreationDO;
import cn.iocoder.yudao.module.eval.service.filingcreation.FilingCreationService;

@Tag(name = "管理后台 - 创建备案")
@RestController
@RequestMapping("/eval/filing-creation")
@Validated
public class FilingCreationController {

    @Resource
    private FilingCreationService filingCreationService;

    @PostMapping("/create")
    @Operation(summary = "创建创建备案")
    @PreAuthorize("@ss.hasPermission('eval:filing-creation:create')")
    public CommonResult<Long> createFilingCreation(@Valid @RequestBody FilingCreationSaveReqVO createReqVO) {
        return success(filingCreationService.createFilingCreation(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新创建备案")
    @PreAuthorize("@ss.hasPermission('eval:filing-creation:update')")
    public CommonResult<Boolean> updateFilingCreation(@Valid @RequestBody FilingCreationSaveReqVO updateReqVO) {
        filingCreationService.updateFilingCreation(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除创建备案")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('eval:filing-creation:delete')")
    public CommonResult<Boolean> deleteFilingCreation(@RequestParam("id") Long id) {
        filingCreationService.deleteFilingCreation(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除创建备案")
                @PreAuthorize("@ss.hasPermission('eval:filing-creation:delete')")
    public CommonResult<Boolean> deleteFilingCreationList(@RequestParam("ids") List<Long> ids) {
        filingCreationService.deleteFilingCreationListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得创建备案")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('eval:filing-creation:query')")
    public CommonResult<FilingCreationRespVO> getFilingCreation(@RequestParam("id") Long id) {
        FilingCreationDO filingCreation = filingCreationService.getFilingCreation(id);
        return success(BeanUtils.toBean(filingCreation, FilingCreationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得创建备案分页")
    @PreAuthorize("@ss.hasPermission('eval:filing-creation:query')")
    public CommonResult<PageResult<FilingCreationRespVO>> getFilingCreationPage(@Valid FilingCreationPageReqVO pageReqVO) {
        PageResult<FilingCreationDO> pageResult = filingCreationService.getFilingCreationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, FilingCreationRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出创建备案 Excel")
    @PreAuthorize("@ss.hasPermission('eval:filing-creation:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportFilingCreationExcel(@Valid FilingCreationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<FilingCreationDO> list = filingCreationService.getFilingCreationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "创建备案.xls", "数据", FilingCreationRespVO.class,
                        BeanUtils.toBean(list, FilingCreationRespVO.class));
    }

}